package com.sciencep.pro.admin.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sciencep.pro.admin.system.common.util.UserUtil;
import com.sciencep.pro.common.domain.QueryRequest;
import com.sciencep.pro.common.domain.ResponseResult;
import com.sciencep.pro.common.exception.CustomException;
import com.sciencep.pro.core.domain.dto.dropdown.ProSelectVO;
import com.sciencep.pro.core.domain.dto.home.*;
import com.sciencep.pro.core.domain.entity.ProBaseInfo;
import com.sciencep.pro.core.domain.entity.Role;
import com.sciencep.pro.core.service.HomeServiceService;
import com.sciencep.pro.core.service.IProBaseInfoService;
import com.sciencep.pro.core.service.IRoleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/home")
public class HomeController {

    @Autowired
    HomeServiceService homeService;

    @Autowired
    IProBaseInfoService proBaseInfoService;

    @Autowired
    IRoleService roleService;

    @Autowired
    private UserUtil userUtil;


    @GetMapping
    @ApiOperation("首页接口")
    public ResponseResult<HomeVO> getHomeVO() throws ParseException, CustomException {

        String userId= userUtil.getCurrentUserDetail().getUserId();

        List<ProSelectVO> proSelectVOS=   proBaseInfoService.getInprocessingProList(userId);
        Set<String> proNos = proSelectVOS.stream().map(ProSelectVO::getProNo).collect(Collectors.toSet());
        List<Role> roles =roleService.getRoleByUserId(userId);
        if(roles.stream().anyMatch(m->m.getRoleName().contains("领导")||m.getRoleName().contains("财务"))){
            proNos.addAll(proBaseInfoService.list().stream().map(ProBaseInfo::getProCode).collect(Collectors.toSet()));
        }
        HomeVO homeVO =new HomeVO();
        homeVO.setOverviewInfoVO(homeService.getOverVieInfo());
        homeVO.setParticipatedInInfoVOList(homeService.getParticipatedInInfoVO(userId));
        homeVO.setManPowerExpendByProjectVO(homeService.getManPowerExpendByProjectVOList(proNos));
        homeVO.setManpowerExpendByRoleVO(homeService.getManpowerExpendByRoleVOList(proNos));
        homeVO.setDailyReportReivewVO(homeService.getDailyReportReivewVOList(userId));
        homeVO.setNewyAssignedVOList(homeService.getNewyAssignedVO(userId));
        homeVO.setMyPowerExpendVOList(homeService.getMyPowerExpandVO(userId));
        homeVO.setMyMandyProgressVOList(homeService.getMyMandyProgressVO(userId));
        homeVO.setYesterdayReviewVO(homeService.getYesterdayReviewVOList(userId));
        homeVO.setTodayReportSubmitTO(homeService.getToday());
        return new ResponseResult<HomeVO>().success(homeVO);

    }

    @GetMapping("/getManpowerUtilization")
    @ApiOperation("首页人天计划接口")
    public ResponseResult<Page<ManpowerUtilizationVO>> getManpowerUtilization(QueryRequest queryRequest){
        return new ResponseResult<Page<ManpowerUtilizationVO>>().success(homeService.getManpowerUtilizationVO(queryRequest));

    }


    @GetMapping("indexA")
    @ApiOperation("首页接口")
    public ResponseResult<OverviewInfoVO> indexA() throws ParseException {
        return new ResponseResult<OverviewInfoVO>().success(homeService.getOverVieInfo());
    }


    @GetMapping("indexB")
    @ApiOperation("首页接口")
    public ResponseResult<List<ParticipatedInInfoVO>> indexB() throws CustomException {
        String userId= userUtil.getCurrentUserDetail().getUserId();
        return new ResponseResult<List<ParticipatedInInfoVO>>().success(homeService.getParticipatedInInfoVO(userId));
    }


    @GetMapping("indexC")
    @ApiOperation("首页接口")
    public ResponseResult<List<ManPowerExpendByProjectVO>> indexC() throws CustomException {
        String userId= userUtil.getCurrentUserDetail().getUserId();
        List<ProSelectVO> proSelectVOS=   proBaseInfoService.getInprocessingProList(userId);
        Set<String> proNos = proSelectVOS.stream().map(ProSelectVO::getProNo).collect(Collectors.toSet());
        List<Role> roles =roleService.getRoleByUserId(userId);
        if(roles.stream().anyMatch(m->m.getRoleName().contains("领导")||m.getRoleName().contains("财务"))){
            proNos.addAll(proBaseInfoService.list().stream().map(ProBaseInfo::getProCode).collect(Collectors.toSet()));
        }
        return new ResponseResult<List<ManPowerExpendByProjectVO>>().success(homeService.getManPowerExpendByProjectVOList(proNos));
    }


    @GetMapping("indexD")
    @ApiOperation("首页接口")
    public ResponseResult<List<ManpowerExpendByRoleVO>> indexD() throws CustomException {
        String userId= userUtil.getCurrentUserDetail().getUserId();
        List<ProSelectVO> proSelectVOS=   proBaseInfoService.getInprocessingProList(userId);
        Set<String> proNos = proSelectVOS.stream().map(ProSelectVO::getProNo).collect(Collectors.toSet());
        List<Role> roles =roleService.getRoleByUserId(userId);
        if(roles.stream().anyMatch(m->m.getRoleName().contains("领导")||m.getRoleName().contains("财务"))){
            proNos.addAll(proBaseInfoService.list().stream().map(ProBaseInfo::getProCode).collect(Collectors.toSet()));
        }
        return new ResponseResult<List<ManpowerExpendByRoleVO>>().success(homeService.getManpowerExpendByRoleVOList(proNos));
    }


    @GetMapping("indexE")
    @ApiOperation("首页接口")
    public ResponseResult<List<DailyReportReivewVO>> indexE() throws CustomException {
        String userId= userUtil.getCurrentUserDetail().getUserId();
        return new ResponseResult<List<DailyReportReivewVO>>().success(homeService.getDailyReportReivewVOList(userId));
    }


    @GetMapping("indexF")
    @ApiOperation("首页接口")
    public ResponseResult<List<NewyAssignedVO>> indexF() throws CustomException {
        String userId= userUtil.getCurrentUserDetail().getUserId();
        return new ResponseResult<List<NewyAssignedVO>>().success(homeService.getNewyAssignedVO(userId));
    }


    @GetMapping("indexG")
    @ApiOperation("首页接口")
    public ResponseResult<List<MyPowerExpendVO>> indexG() throws CustomException {
        String userId= userUtil.getCurrentUserDetail().getUserId();
        return new ResponseResult<List<MyPowerExpendVO>>().success(homeService.getMyPowerExpandVO(userId));
    }


    @GetMapping("indexH")
    @ApiOperation("首页接口")
    public ResponseResult<List<MyMandyProgressVO>> indexH() throws CustomException {
        String userId= userUtil.getCurrentUserDetail().getUserId();
        return new ResponseResult<List<MyMandyProgressVO>>().success(homeService.getMyMandyProgressVO(userId));
    }


    @GetMapping("indexI")
    @ApiOperation("首页接口")
    public ResponseResult<YesterdayReviewVO> indexI() throws CustomException {
        String userId= userUtil.getCurrentUserDetail().getUserId();
        return new ResponseResult<YesterdayReviewVO>().success(homeService.getYesterdayReviewVOList(userId));
    }


    @GetMapping("indexJ")
    @ApiOperation("首页接口")
    public ResponseResult<TodayReportSubmitTO> indexJ() throws CustomException {
        String userId= userUtil.getCurrentUserDetail().getUserId();
        return new ResponseResult<TodayReportSubmitTO>().success(homeService.getToday());
    }

}
